<template>
    <div>
        <!-- 图片详情页 -->
        <el-dialog
            :title="photoDetail.title"
            :visible.sync="dialogVisible"
            width="90%"
            top="5vh"
            :before-close="_handleClose"
        >
            <template slot="title"
                ><img
                    :src="photoDetail.userAvatar"
                    @click="_toDetail(photoDetail.userId)"
                    class="photo_detail_user_avatar"
                />
                <div class="photo_detail_nick">{{ photoDetail.nick }}</div>
                <div style="float:right;height:28px;width:auto;margin-right:100px;">
                    <el-button size="mini" 
                        type="danger"
                        @click="_collect()"
                        v-if="isCollected"
                        >已收藏</el-button
                    >
                    <el-button size="mini" type="danger" @click="_collect()" v-else
                        >收藏</el-button
                    >
                </div>
            </template>
            <img :src="photoDetail.link" class="photo_detail_img" />
            <div>
                <div class="photo_detail_title">
                    {{ photoDetail.title }}
                </div>
                <!-- 点赞 -->
                <div style="margin-top:20px;margin-left:20px" >
                    <div
                        class="photo_praise_icon"
                        @click="_updateApprovalCountsById(photoDetail.photoId)"
                    ></div>
                    {{ photoDetail.approvalCounts }}
                </div>
                <hr />
                <div class="photo_detail_information">
                    {{ photoDetail.information }}
                </div>
                <hr />
                <el-row>
                    <div class="photo_detail_comment_counts">
                        {{
                            commentList === null ? 0 : commentList.length
                        }}条评论
                    </div>
                    <div>
                        <el-form
                            :inline="true"
                            :model="commentForm"
                            class="comment_input"
                            ref="commentFormRef"
                        >
                            <el-form-item
                                prop="content"
                                :rules="[
                                    {
                                        required: true,
                                        message: '请输入评论',
                                        trigger: 'blur',
                                    },
                                ]"
                            >
                                <el-input
                                    style="width: 500px"
                                    v-model="commentForm.content"
                                    placeholder="善语结善缘，恶语伤人心~"
                                ></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button
                                    type="primary"
                                    @click="_comment('commentFormRef')"
                                    >评论</el-button
                                >
                            </el-form-item>
                        </el-form>
                    </div>
                    <el-card class="comment_container">
                        <div class="photo_detail_comment_list">
                            <el-empty
                                v-if="commentList === null"
                                description="暂无评论"
                            >
                            </el-empty>
                            <div
                                v-for="(item, index) in commentList"
                                :key="index"
                                class="photo_detail_comment_ui"
                            >
                                <img
                                    @click="_toDetail(item.userId)"
                                    :src="item.userAvatar"
                                    class="photo_detail_comment_user_avatar"
                                />
                                {{ item.nick }}&nbsp;&nbsp;&nbsp;&nbsp;
                                {{ item.commentTime }}
                                <div>
                                    {{ item.content }}
                                </div>
                            </div>
                        </div>
                    </el-card>
                </el-row>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import commentApi from '../request/commentApi';
import imageApi from '../request/imageApi';
import collectionApi from '../request/collectionApi';
import userApi from '../request/userApi';
export default {
    name: 'PhotoDetail',
    props: {
        // 打开图片详情页
        dialogVisible: {
            type: Boolean,
            default: false,
        },
        // 图片id
        photoId: {
            type: Number,
            default: 1,
        },
    },
    data() {
        return {
            // 图片详情
            photoDetail: {},
            // 评论列表
            commentList: [],
            // 评论表单
            commentForm: {},
            // 是否收藏
            isCollected: false,
        };
    },

    methods: {
        // 评论
        _comment(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let data = {
                        content: this.commentForm.content,
                        photoId: this.photoDetail.photoId,
                    };
                    commentApi.addComment(data).then((res) => {
                        if (res.code === 200) {
                            this.$message({
                                message: '评论成功',
                                type: 'success',
                            });
                            this.commentForm.content = '';
                            commentApi
                                .getComments(this.photoDetail.photoId)
                                .then((res) => {
                                    if (res.code === 200) {
                                        this.commentList = res.data;
                                    } else {
                                        this.commentList = null;
                                    }
                                });
                        } else {
                            this.$message({
                                message: '请先登录',
                                type: 'info',
                            });
                        }
                    });
                } else {
                    return false;
                }
            });
        },
        // 前往个人详情页面
        _toDetail(id) {
            if (id === this.$store.state.userId) {
                this.$router.push('/personal');
                return;
            }
            this.$router.push({
                path: '/user/' + id,
            });
        },
        // 关闭图片详情页
        _handleClose() {
            this.$emit('close');
        },
        // 获取评论列表
        _getComments(id) {
            commentApi.getComments(id).then((res) => {
                if (res.code === 200) {
                    this.commentList = res.data;
                } else {
                    this.commentList = null;
                }
            });
        },
        //获取图片详情
        _getPhotoById(id) {
            imageApi.getPhotoById(id).then((res) => {
                this.photoDetail = res.data;
            });
        },
        //增加浏览量
        _addViewCount(id) {
            imageApi.addPhotoViewCounts(id);
        },
        //是否收藏
        _isCollected(id) {
            collectionApi.isCollected(id).then((res) => {
                console.log(res);
                if (res.code === 200) {
                    this.isCollected = true;
                } else {
                    this.isCollected = false;
                }
            });
        },
        // 收藏
        _collect() {
            userApi.checkLogin().then((res) => {
                if (res.code !== 200) {
                    this.$message.error('请先登录');
                    return;
                } else {
                    collectionApi.toggleCollect(this.photoId).then((res) => {
                        if (res.code === 200) {
                            this._isCollected(this.photoId);
                            this.$message.success('收藏成功');
                        } else {
                            this._isCollected(this.photoId);
                            this.$message.success('取消收藏成功');
                        }
                    });
                }
            });
        },
        // 点赞
        _updateApprovalCountsById(photoId) {
            if (!window.sessionStorage.getItem('token')) {
                this.$message.info('请先登录');
                return;
            }
            let data = {
                photoId: photoId,
            };
            imageApi.updateApprovalCountsById(data).then((res) => {
                if (res.code == 200) {
                    this.$message.success('点赞成功');
                    this._getPhotoById(this.photoId);
                } else {
                    this.$message.error(res.msg);
                }
            });
        },
    },
    watch: {
        // 监听图片id
        dialogVisible: function (val) {
            if (val) {
                this._addViewCount(this.photoId);
                this._getPhotoById(this.photoId);
                this._getComments(this.photoId);
                this._isCollected(this.photoId);
            }
        },
    },
};
</script>
<style scoped>
.photo_detail_user_avatar {
    width: 40px;
    height: 40px;
    /* margin: 0px 10px 10px; */
    border-radius: 50%;
    float: left;
    cursor: pointer;
}
.photo_detail_nick {
    float: left;
    margin-top: 10px;
    margin-left: 10px;
    font-size: 12px;
}
.photo_detail_img {
    width: auto;
    height: 500px;
    margin: 0 auto;
    margin-top: 10px;
    display: block;
}
.photo_detail_title {
    margin: 30px auto;
    padding-left: 30px;
    text-align: left;
    font-size: 20px;
    font-weight: bold;
}
.photo_detail_comment_counts {
    margin: 30px auto;
    padding-left: 30px;
    text-align: left;
    font-size: 20px;
    color: black;
    font-weight: bold;
}
.photo_detail_comment_list {
    width: 100%;
    /* height: 30px; */
}
.photo_detail_comment_user_avatar {
    width: 40px;
    height: 40px;
    /* margin: 0px 10px 10px; */
    border-radius: 50%;
    float: left;
    margin-right: 15px;
    cursor: pointer;
}
.photo_detail_comment_ui {
    width: 100%;
    height: 40px;
    margin-left: 30px;
    margin-top: 30px;
}
.comment_container {
    height: 500px;
    width: 100%;
    overflow-y: auto;
}
.comment_input {
    text-align: center;
    margin-top: 10px;
    width: 100%;
}
/* 点赞 */
.photo_praise_icon {
    float: left;
    /* margin: 0 auto; */
    margin-right: 10px;
    cursor: pointer;
    background: url(../assets/fonts/点赞.png) no-repeat;
    background-size: 100% 100%;
    width: 20px;
    height: 20px;
}
</style>